home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_152 / runback / runback.c < prev   
C/C++ Source or Header  |  1992-05-06  |  6KB  |  199 lines

  1. /* runbackground.c */
  2. /* Author:  Rob Peck.  5/9/86 */
  3.  
  4. /* Modified 5/21/88 by Dan Barrett to include PATH searching; added
  5.  * the functions FullPathOf() and FindIt(), largely taken from C.
  6.  * Scheppner's "which.c" program.
  7.  *
  8.  * Also, a few "#ifdef AZTEC_C" lines had to be added.  It seems that
  9.  * Aztec C parses the command line differently from the way Lattice
  10.  * does with respect to arguments in quotes.  When I compiled this
  11.  * program with Aztec, all the quotes in quoted arguments were 
  12.  * disappearing.  I re-insert them around any argument that has a
  13.  * space character (' ') in it.
  14. */
  15.  
  16. /*#define DEBUG*/     /* Uncomment this line for debugging. */
  17.  
  18. #define EQUAL(a,b)    !strcmp(a,b)
  19.     
  20. #include "exec/types.h"
  21. #include "exec/memory.h"
  22. #include "libraries/dosextens.h"
  23.  
  24. extern struct FileHandle *Open();
  25. extern struct FileLock *Lock();
  26. extern VOID *AllocMem();
  27.     
  28. main(argc, argv)
  29. int argc;
  30. char *argv[];
  31. {    
  32.     LONG success, delaywillbe;
  33.     UBYTE commandstring[255];
  34.     char *test, *filename;
  35.     LONG fromparm;
  36.     struct FileInfoBlock *fib;
  37.     struct FileHandle *nilfh;    /* NOTE: will hang around until next reset */
  38.     struct FileLock *lock, *FullPathOf();
  39. #ifdef AZTEC_C
  40.     int hasSpace=0;        /* Does an string have a space in it. */
  41. #endif
  42.     
  43.     fib = NULL;            /* No file info block so far. */
  44.     delaywillbe = 1;
  45.  
  46.     if(argc < 2 || EQUAL(argv[1],"?")) {
  47. usage:
  48.     printf("Usage: RUNBACKGROUND [ -<loaddelay>] <name> [<parm(s)>]\n");
  49.     printf("          where optional loaddelay is 0-9,\n");
  50.     printf("          specified in seconds for the CLI\n");
  51.     printf("          to sleep, waiting for task to load\n");
  52.     printf("          (minimizes inter-task disk-thrashing)\n");
  53.     if(fib)
  54.         FreeMem(fib, sizeof(struct FileInfoBlock));
  55.     exit(0);
  56.     }
  57.  
  58.     /* See if there is a delay parameter present */
  59.  
  60.     test = argv[1];
  61.  
  62.     if(*test++ == '-') {
  63.     filename = argv[2];    /* argv[1] is delay so argv[2] is file  */
  64.     fromparm = 3;        /* Copy parms from 3 to end          */
  65.  
  66.     if(*test >= '0' && *test <= '9')
  67.         delaywillbe = 1 + (50 * (*test - '0')); 
  68.  
  69.     if (argc < 3)
  70.         goto usage; /* Only a delay, and no filename!! */
  71.  
  72.     argc--;        /* one less parm to copy */
  73.     }
  74.     else {
  75.     filename = argv[1];
  76.     fromparm = 2;        /* Copy parms from 2 to end         */
  77.     }
  78.  
  79.     /* Now see if the file exists!  If not, it can crash the background
  80.      * CLI and take the system along with it.
  81.      */
  82.  
  83.     lock = FullPathOf(filename);
  84.     if(!lock) {
  85.     printf("%ls: Command not found\n",filename);
  86.     goto usage;
  87.     }
  88.     else {
  89.     /* If file exists, it better be a file and not a directory */
  90.  
  91. /*DJB*/    /* With my (Dan's) changes, a file that is not found at all
  92.      * is falsely identified as a directory.  Irritating, but not
  93.      * fatal.
  94.      */
  95.  
  96.     /* Unfortunately, it is difficult to tell if it is an executable
  97.      * file.  If not executable, we'll still get blown out of the
  98.      * water, but that is up to the user to do it right!
  99.      */
  100.  
  101.     fib =  (struct FileInfoBlock *)
  102.             AllocMem(sizeof(struct FileInfoBlock),MEMF_CLEAR);
  103.     if(!fib) {
  104.         UnLock(lock);
  105.         printf("Ran out of memory!\n");
  106.         exit(0);
  107.     }
  108.     else {
  109.         success = Examine(lock,fib);
  110.         if(fib->fib_DirEntryType > 0) {
  111. /*        printf("%ls is a directory, not a file!\n",filename); */
  112. /*DJB*/        printf("Cannot open %ls... maybe a directory?\n",filename);
  113.         goto usage;
  114.         }
  115.     }
  116.        FreeMem(fib, sizeof(struct FileInfoBlock));
  117.     UnLock(lock);
  118.     }
  119.  
  120.     nilfh = Open("NIL:",MODE_NEWFILE); /* will always succeed */
  121.  
  122.     strcpy( &commandstring[0], "RUN >NIL: <NIL: " );
  123.     strcat( &commandstring[0], filename);  
  124.  
  125.     /* REMOVE THIS NEXT LINE IF YOU WANT TO INCLUDE REDIRECTION IN
  126.      * THE COMMAND LINE FOR RUNBACKGROUND.   (The line was installed
  127.      * to assure that something like "RUNBACKGROUND date ?" would
  128.      * not crash the system.  "Date ?" is expecting to have an interactive
  129.      * CLI, and unless it is specifically told to direct its output to nil:
  130.      * it causes a crash.  If the next line is removed, and you are careful
  131.      * about putting only NON-interactive commands in the command line,
  132.      * everything should be ok.  Notice that if you do not specify a 
  133.      * non-interactive file handle (named_disk_file or NIL:) for your
  134.      * program, it may still prevent the originating CLI from going away
  135.      * until your program ends.  Also note that specifying two instances 
  136.      * of the same redirection (">somewhere" or "<somewhere") for a 
  137.      * background task crashes.
  138.      */
  139.  
  140.     strcat( &commandstring[0], " >NIL: <NIL: ");
  141.  
  142.     argc--;
  143.  
  144.     while(--argc > 0) {     /* Rebuild parameter string for passing it on */
  145.     strcat( &commandstring[0], " ");    /* add a blank */
  146.  
  147. #ifdef AZTEC_C
  148.     hasSpace = HasASpace(argv[fromparm]);    /* Quoted argument?     */
  149.     if (hasSpace)                /* Then quote it again! */
  150.         strcat( &commandstring[0], "\"");
  151. #endif
  152.  
  153.     strcat( &commandstring[0], argv[fromparm++]);
  154.  
  155. #ifdef AZTEC_C
  156.     if (hasSpace)
  157.         strcat( &commandstring[0], "\"");
  158. #endif
  159.  
  160.     }
  161.  
  162. #ifdef DEBUG
  163.     printf("Execute %s\n", &commandstring[0]);
  164. #else
  165.     success = Execute( &commandstring[0] , nilfh, nilfh);
  166. #endif
  167.  
  168.     /* The full command passed to Execute now looks like this:
  169.      *
  170.      *    "RUN >NIL: <NIL: FILENAME >NIL: <NIL: PARAMETER(s)"
  171.      *
  172.      */
  173.  
  174.     if(success) {
  175.     printf("Started %ls as a background task\n",filename);
  176.  
  177.     /* Execute, in this case, returns IMMEDIATELY.  The process
  178.      * that is loading the code that is to be run as a background
  179.      * process is working to get everything in and started.  
  180.      */
  181.     }
  182.     /* Now, to minimize thrashing between tasks, lets put this task to 
  183.      * sleep so that the each task actually gets a chance to load.
  184.      */
  185.     Delay(delaywillbe);
  186. }
  187.  
  188.     
  189. struct FileLock *FullPathOf(filename)
  190. char *filename;
  191. {
  192.     struct FileLock *lock;
  193.     char realname[256], *FindIt();
  194.  
  195.     strcpy(realname, FindIt(filename));
  196.     lock = Lock(realname,ACCESS_READ);
  197.     return(lock);
  198. }
  199.